package com.a0soft.gphone.aTruffleHog.wnd;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.analytic.AnalyticsUtil;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.gio.ui.BaseActivity;
import com.a0soft.gphone.aTruffleHog.gio.util.UIUtils;
import com.a0soft.gphone.aTruffleHog.util.aDebug;

import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.ImageView;

public class DashboardWnd extends BaseActivity {
    private DialogFragment m_frgDlg;
    private static final String LOG_TAG = DashboardWnd.class.getSimpleName();
    
    @Override protected void 
    onCreate(Bundle bdSavedInstanceState)
    {
        super.onCreate(bdSavedInstanceState);
        
        setContentView(R.layout.dashboard_wnd);

        if (UIUtils.isHoneycombTablet(this)) {
            getActionBar().hide();
        }
        else {
            getActivityHelper().setupActionBar(getTitle(), 0);
        }
        
        ImageView ivLogo = (ImageView)findViewById(R.id.logo);
        if (ivLogo != null) {
            ((BitmapDrawable)ivLogo.getBackground()).setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
        }
        
        //Pref.AddOtherDevice(this, "samsung-gt-i9100", "GT-I9100");
        //Pref.AddOtherDevice(this, "htc evo 3d", "HTC EVO 3D");
    }

    @Override protected void 
    onDestroy()
    {
        AnalyticsUtil.GetInstance(this).Stop();

        super.onDestroy();
    }

    @Override protected void 
    onPostCreate(Bundle bdSavedInstanceState) 
    {
        super.onPostCreate(bdSavedInstanceState);
        //getActivityHelper().setupHomeActivity();
    }
    
    @Override protected void 
    onResume()
    {
        super.onResume();
        
        DeviceInfo di = DeviceInfo.GetInstance(this);
        
        if (!di.IsReady()) {
            //build device information and show progress dialog when building
            final String strTag = ProgressDlgFrg.class.getSimpleName();
            m_frgDlg = (DialogFragment)getSupportFragmentManager().findFragmentByTag(strTag);
            if (aDebug.ENABLED)
                aDebug.d(LOG_TAG, "m_frgDlg found:" + (m_frgDlg != null));

            if (m_frgDlg == null) {
                m_frgDlg = ProgressDlgFrg.NewInstance(0, R.string.device_info_building);
                m_frgDlg.show(getSupportFragmentManager(), strTag);
            }
            
            di.Init(false, new DeviceInfo.OnListener() {            
                @Override public void 
                OnDeviceInfoReady(DeviceInfo di)
                {
                    if (aDebug.ENABLED)
                        aDebug.d(LOG_TAG, "OnDeviceInfoReady()");
                    
                    if (m_frgDlg != null) {
                        if (m_frgDlg.getFragmentManager() != null)
                            m_frgDlg.dismiss();
                        m_frgDlg = null;
                    }
                    
                    _OnDeviceInfoReady();
                }
            });
        }
        else {
            _OnDeviceInfoReady();
        }
    }

    private void
    _OnDeviceInfoReady()
    {
        AnalyticsUtil au = AnalyticsUtil.GetInstance(this);
        if (!au.IsStarted())
            au.Start();
    }
    
    @Override protected void 
    onPause()
    {
        DeviceInfo.GetInstance(this).Stop();
        if (m_frgDlg != null && m_frgDlg.getFragmentManager() != null) {
            m_frgDlg.dismiss();
            m_frgDlg = null;
        }
        
        super.onPause();
    }
}
